package com.sc.nft.controller;

import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.controller.vo.UserBankCardVO;
import com.sc.nft.dao.StaticDataDao;
import com.sc.nft.entity.StaticData;
import com.sc.nft.entity.vo.ApplyBindCardVO;
import com.sc.nft.entity.vo.CardInfoVO;
import com.sc.nft.enums.PayChannelEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.UserBankCardService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.Result;
import com.sc.nft.util.BankCardUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Locale;
import java.util.Objects;

/**
 * 服务控制器
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-05-29 10:36:55
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/userBank")
@Api(tags = "用户銀行卡")
public class UserBankController {

    private final UserBankCardService userBankCardService;
    private final StaticDataDao staticDataDao;

    @ApiOperation(value = "获取银行卡信息", notes = "获取银行卡信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getCardInfo", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<CardInfoVO> getCardInfo(@Valid @RequestBody GetCardInfoRequest request) throws GlobalException {
        return Result.success(userBankCardService.getCardInfo(request.getCardNo()));
    }

    @ApiOperation(value = "申请绑卡", notes = "申请绑卡", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/applyBindCard", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<ApplyBindCardVO> applyBindCard(@Valid @RequestBody ApplyBindBankCardRequest request) throws GlobalException {
        if (request.getChannel()== PayChannelEnum.HF_PAY) {
            throw new GlobalRunTimeException("该钱包已下线");
        }
        // 把身份证号中的小写最后一位x换位大写X
//        request.setCertificateNo(request.getCertificateNo().toUpperCase(Locale.ROOT));
//        if (!BankCardUtil.IdentityCardVerification(request.getCertificateNo())) {
//            return Result.fail(ErrorCode.IDENTITY_NO_ERROR);
//        }
        if (!BankCardUtil.checkBankCard(request.getCardNo())) {
            return Result.fail(ErrorCode.BANK_CARD_ERROR);
        }
        return Result.success(userBankCardService.applyBindCard(UserPool.getUser(), request.getCardNo(), request.getPhoneNo(),request.getChannel()));
    }

    @ApiOperation(value = "新生支付绑卡", notes = "新生支付绑卡", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/hnaBindCard", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result hnaBindCard(@Valid @RequestBody ApplyBindBankCardRequest request) {
        StaticData faceControlButton = staticDataDao.getByType("face_control_button");
        if (Objects.nonNull(faceControlButton) && "1".equals(faceControlButton.getValue())) {
            userBankCardService.hnaBindCardNew(UserPool.getUser(), request.getCardNo(), request.getPhoneNo());
        }else {
            userBankCardService.hnaBindCard(UserPool.getUser(), request.getCardNo(), request.getPhoneNo());
        }

        return Result.success();
    }

    @ApiOperation(value = "确认绑卡", notes = "确认绑卡", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/confirmBindCard", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result confirmBindCard(@Valid @RequestBody ConfirmBindBankCardRequest request) throws GlobalException {
        userBankCardService.confirmBindCard(UserPool.getUser(), request.getId(), request.getSmsCode());
        return Result.success();
    }

    @ApiOperation(value = "用户银行卡列表", notes = "用户银行卡列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/cardList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<UserBankCardVO> cardList(@RequestBody PaymentChannelRequest request) throws GlobalException {
        return Result.successColl(userBankCardService.getByUserIdAndChannel(UserPool.getUser().getId(),request.getChannel()), UserBankCardVO.class);
    }
//    @ApiOperation(value = "用户银行卡列表", notes = "用户银行卡列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @PostMapping(value = "/cardList", produces = MediaType.APPLICATION_JSON_VALUE)
//    @UserLogin
//    public Result<UserBankCardVO> cardList() throws GlobalException {
//        return Result.successColl(userBankCardService.getByUserIdAndChannel(UserPool.getUser().getId(), PayChannelEnum.SAND), UserBankCardVO.class);
//    }
    @ApiOperation(value = "解绑", notes = "解绑", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/unBind", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<UserBankCardVO> confirmBindCard(@Valid @RequestBody IdRequest request) throws GlobalException {
        userBankCardService.unBind(UserPool.getUser(), request.getId());
        return Result.success();
    }
}